package com.taoyibao.hongcanting.ui.home.delegate;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.delegate.ColorToolbarDelegate;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.ApiRoute;
import com.taoyibao.hongcanting.model.UserInfoModel;
import com.taoyibao.hongcanting.ui.home.activity.ShoppingCartActivity;
import com.taoyibao.hongcanting.ui.login.activity.LoginActivity;
import com.taoyibao.hongcanting.ui.mine.activity.MineCouponActivity;
import com.taoyibao.hongcanting.ui.mine.activity.MineOrderActivity;
import com.taoyibao.hongcanting.ui.mine.activity.SettingActivity;
import com.taoyibao.hongcanting.ui.mine.activity.UserInfoActivity;
import com.taoyibao.hongcanting.ui.mine.activity.WalletActivity;
import com.taoyibao.hongcanting.ui.webview.CommonWebViewActivity;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.taoyibao.hongcanting.utils.GlideUtil;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author TangAnna
 * @description: 我的模块
 * @date :${DATA} 14:51
 */
public class MineFragmentDelegate extends ColorToolbarDelegate implements MultiItemTypeAdapter.OnItemClickListener, View.OnClickListener, ColorToolbarDelegate.OnRightClickListener {
    /**
     * 列表的文字
     */
    private List<String> mTitleData = new ArrayList<String>(Arrays.asList("购物车", "我的订单", "我的钱包", "我的卡券", "关于我们"));
    /**
     * 列表的图片资源
     */
    private List<Integer> mImgData = new ArrayList<>(Arrays.asList(R.drawable.mine_gouwuche, R.drawable.mine_daizhifu,
            R.drawable.mine_qianbao, R.drawable.mine_kajuan, R.drawable.mine_guanyuwomen));
    private RecyclerView mRecyclerView;
    private CommonAdapter mAdapter;

    @Override
    public int getRootLayoutId() {
        return R.layout.fragment_mine;
    }


    @Override
    public void initWidget() {
        super.initWidget();
        mRecyclerView = get(R.id.rv_mine_list);
        setTitle("我的");
        setRightText(CodeUtils.getIdString(R.string.string_seting));
        setRightImg(R.drawable.shezhi);
        setOnRightClickListener(this);
        get(R.id.layout_mine_peopleInfo).setOnClickListener(this);
        setTitleData();
    }

    /**
     * 设置列表数据
     */
    public void setTitleData() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(mAdapter = new CommonAdapter<String>(getActivity(), R.layout.item_mine, mTitleData) {
            @Override
            protected void convert(ViewHolder holder, String s, int position) {
                TextView tvTitle = holder.getView(R.id.tv_itemMine_title);
                ImageView ivImg = holder.getView(R.id.iv_itemMine_img);
                View viewBg = holder.getView(R.id.view_item_bg);
                View viewLine = holder.getView(R.id.view_item_line);
                //设置文字
                tvTitle.setText(s);
                //设置图片
                ivImg.setImageResource(mImgData.get(position));
                if (position % 2 != 0) {
                    viewBg.setVisibility(View.GONE);
                    viewLine.setVisibility(View.GONE);
                } else {
                    viewBg.setVisibility(View.VISIBLE);
                    viewLine.setVisibility(View.VISIBLE);
                }
            }
        });
        mAdapter.setOnItemClickListener(this);
    }

    /**
     * 点击事件
     *
     * @param view
     * @param holder
     * @param position
     */
    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        if (SpUtils.getLoginStatus()) {//已登录状态
            switch (position) {
                case 0://购物车
                    ShoppingCartActivity.open(getActivity(), "MineFragmentDelegate");
                    break;
                case 1://待支付
                    getActivity().startActivity(new Intent(getActivity(), MineOrderActivity.class));
                    break;
                case 2://我的钱包
                    WalletActivity.open(getActivity());
                    break;
                case 3://我的卡券
                    getActivity().startActivity(new Intent(getActivity(), MineCouponActivity.class));
                    break;
                case 4://关于我们
                    CommonWebViewActivity.open(getActivity(), "关于我们", ApiRoute.ABOUT_WEB_PATH);
                    break;
            }
        } else {//未登录状态
            LoginActivity.open(getActivity(), "");
        }
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_mine_peopleInfo://个人资料
                if (SpUtils.getLoginStatus()) {//登录
                    getActivity().startActivity(new Intent(getActivity(), UserInfoActivity.class));
                } else {
                    LoginActivity.open(getActivity(), "");
                }
                break;
        }
    }

    /**
     * 显示用户信息
     *
     * @param userInfoModel
     */
    public void setUserData(UserInfoModel userInfoModel) {

        if (SpUtils.getLoginStatus() && !TextUtils.isEmpty(SpUtils.getUserPhone())) {//已登录
            ((TextView) get(R.id.tv_mine_userPhone)).setText(CodeUtils.formatHintPhone(SpUtils.getUserPhone()));
            if (!TextUtils.isEmpty(userInfoModel.avatar_url)) {
                GlideUtil.getInstance().loadDefaultCircleHead(getActivity(), userInfoModel.avatar_url, (ImageView) get(R.id.iv_mine_head));
            } else {
                ((ImageView) get(R.id.iv_mine_head)).setImageResource(R.drawable.default_touixiang);
            }
            //用户昵称
            if (!TextUtils.isEmpty(userInfoModel.nickname)) {
                get(R.id.tv_mine_userNick).setVisibility(View.VISIBLE);
                ((TextView) get(R.id.tv_mine_userNick)).setText(userInfoModel.nickname);
            } else {
                get(R.id.tv_mine_userNick).setVisibility(View.GONE);
            }

        } else {//未登录
            ((TextView) get(R.id.tv_mine_userPhone)).setText(CodeUtils.getIdString(R.string.string_notLoggedIn));
            //清除头像图标
            ((ImageView) get(R.id.iv_mine_head)).setImageResource(R.drawable.default_touixiang);
            get(R.id.tv_mine_userNick).setVisibility(View.GONE);
        }
    }

    @Override
    public void onRightClick(View view) {
        if (SpUtils.getLoginStatus()) {
            getActivity().startActivity(new Intent(getActivity(), SettingActivity.class));
        } else {
            LoginActivity.open(getActivity(), "");
        }
    }
}
